﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using WeForms.View;

namespace WeForms
{
    public class WeTabSideBarViewButton : WeTabSideBarButton
    {
        /// <summary>
        /// Gets or sets the view to be displayed after click
        /// </summary>
        public string View { get; set; }

        [Browsable(false)]
        public override bool Checked
        {
            get
            {
                WeTabContainer container = FindContainer();
                if (container != null)
                    return View == container.CurrentViewId;
                return base.Checked;
            }
            set
            {
                base.Checked = value;
                Invalidate();
            }
        }

        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);
            Console.WriteLine("ViewClick");

            WeTabContainer container = FindContainer();
            if (container == null)
            {
                Console.WriteLine("No Container in Hierarchy");
                return;
            }
            container.DisplayView(View);
        }

        private WeTabContainer FindContainer()
        {
            // TODO: Mono doesn't  set parent within ControlCollection, we need to set parents within WeTabSideBarControlList
            Control parent = Parent;

            while (parent != null)
            {
                Console.WriteLine("Parent {0}", parent.GetType().FullName);
                if (parent is WeTabContainer)
                    return (WeTabContainer)parent;
                parent = parent.Parent;
            }

            Console.WriteLine("Parent is null");

            return null;
        }

    }
}
